const {
  $util,
} = getApp();

const date = new Date();
const yearsColumn = [];
const monthsColumn = [];

for (let i = date.getFullYear(); i >= 2000; i--) {
  yearsColumn.push(i);
}

for (let i = 1; i <= 12; i++) {
  monthsColumn.push(i);
}

Page({
  data: {
    rankidx: 0, // 0总排行 1支部排行
    showPicker: false,
    yearsColumn: yearsColumn,
    monthsColumn: monthsColumn,
    orderWay: 0, // 0全部  1 年份  2 月份
    all: 1,
    year: 0,
    month: 0,
    searchBtnTxt: '全部',
    imgUrl: $util.getImgUrl(),
    nav: [{ id: 0, title: '总排名' }, { id: 1, title: '支部排名' }],
  },
  radioChange(e) {
    this.setData({
      orderWay: e.detail.value,
    });
    // 赋默认值
    let { orderWay } = this.data;
    if (orderWay == 0) {
      this.setData({
        all: 1,
        year: 0,
        month: 0,
      });
    } else if (orderWay == 1) {
      this.setData({
        all: 0,
        year: this.data.yearsColumn[0],
        month: 0,
      });
    } else if (orderWay == 2) {
      this.setData({
        all: 0,
        year: this.data.yearsColumn[0],
        month: this.data.monthsColumn[0],
      });
    }
  },
  bindSortChange(e) {
    let { orderWay } = this.data;
    // 按年搜索
    if (orderWay == 0) {
      this.setData({
        all: 1,
        year: 0,
        month: 0,
      });
    } else if (orderWay == 1) {
      this.setData({
        all: 0,
        year: this.data.yearsColumn[e.detail.value],
        month: 0,
      });
    } else if (orderWay == 2) {
      this.setData({
        all: 0,
        year: this.data.yearsColumn[e.detail.value[0]],
        month: this.data.monthsColumn[e.detail.value[1]],
      });
    }
  },
  showPicker() {
    this.setData({
      showPicker: true,
    });
  },
  hidePicker() {
    this.setData({
      showPicker: false,
    });
  },
  onLoad(opts) {
    this.loadData();
    wx.setNavigationBarTitle({
      title: opts.barTitle && decodeURIComponent(opts.barTitle) || '积分排行',
    });
  },
  onShow() {
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    });
  },
  loadData() {
    let { orderWay, year, month } = this.data;
    month = month < 10 ? '0' + month : month;

    // 按年搜索
    if (orderWay == 0) {
      this.setData({
        searchBtnTxt: '全部',
      });
    } else if (orderWay == 1) {
      this.setData({
        searchBtnTxt: year + '年',
      });
    } else if (orderWay == 2) {
      this.setData({
        searchBtnTxt: year + '年' + month + '月',
      });
    }

    $util.request({
      url: 'entry/wxapp/partyman.integral.getpoints',
      data: {
        type: this.data.rankidx,
        year: this.data.year,
        month: this.data.orderWay == 2 ? this.data.year + '-' + month : 0,
        all: this.data.all,
      },
      success: (res) => {
        const _user = res.sum;
        _user.imgSrc = res.sum.avator;
        this.setData({
          list: res.data,
          userInfo: _user,
          isLoad: true,
        });
      },
    });
  },
  bindchange(e) {
    this.setData({
      list: [],
      rankidx: e.detail.obj.id,
      isLoad: false,
    });
    this.loadData();
  },
  bindYearChange(e) {
    this.hidePicker();
    this.setData({
      page: 1,
      isLoad: false,
    }, () => {
      this.loadData();
    });
  },
});
